package org.xtext.runtime.plan;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.xtext.runtime.formula.Predicate;
import org.xtext.runtime.interpreter.Agent;
import org.xtext.runtime.term.Variable;
import org.xtext.runtime.util.Bindings;

/**
 * Plan Step implementation for starting / stopping teleoreactive plans
 *  
 * @author rem
 *
 */
public class TRControlStep implements IPlanStep {
	public static final String START 			= "start";
	public static final String STOP 			= "stop";
	
	String mode;
	Predicate identifier;
	
	public TRControlStep(String mode, Predicate identifier) {
		this.mode = mode;
		this.identifier = identifier;
	}
	
	@Override
	public String toString(String string) {
		return string + mode + "(" + identifier + ")";
	}

	@Override
	public String toFullString(String string) {
		return toString(string);
	}

	@Override
	public Set<Variable> variables() {
		Set<Variable> variables = new HashSet<Variable>();
		variables.addAll(identifier.variables());
		return variables;
	}

	@Override
	public void handle(Agent agent, PlanStack planStack) {
		if (mode.equals(START)) agent.startFunction(identifier);
		else agent.stopFunction(identifier);
	}

	@Override
	public TRControlStep copy(Map<String, Variable> vars) {
		return new TRControlStep(mode, identifier.copy(vars));
	}

	@Override
	public TRControlStep apply(Bindings bindings) {
		identifier.apply(bindings);
		return this;
	}

	@Override
	public void reset() {
	}
}
